<?php
// +----------------------------------------------------------------------
// | B5LaravelCMF
// +----------------------------------------------------------------------
// | Author: 李恒 <357145480@qq.com>
// +----------------------------------------------------------------------
namespace App\Http\Controllers\Admin;

use App\Cache\WebPosCache;
use App\Services\WebAdService;
use App\Services\WebPosService;
use App\Services\WebSiteService;
use Illuminate\Http\Request;

class WebAdController extends Backend
{
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->service = new WebAdService();
        if(IS_GET && !IS_AJAX)  {
            view()->share('siteList',(new WebSiteService())->getSiteList());
            view()->share('posList',(new WebPosService())->getListGroupByWeb());
        }
    }

    public function index()
    {
        if(IS_POST){
            $website=\request()->input('website','');
            if(!$website) return message('操作成功',true,[]);
            $param=\request()->input('where',[]);
            if(!$param || !isset($param['pos_id']) || !$param['pos_id']){
                $posList=(new WebPosService())->getListGroupByWeb();
                $posWeb=$posList[$website]??[];
                if(!$posWeb) return message('操作成功',true,[]);
                $posIdArr=[];
                foreach ($posWeb as $val){
                    $posIdArr[]=$val['id'];
                }
                if(count($posIdArr)==1){
                    $where=[['pos_id','=',$posIdArr[0]]];
                }else{
                    $where=[['pos_id','in',$posIdArr]];
                }
                return $this->service->getList(false,$where);
            }
        }
        return parent::index(); // TODO: Change the autogenerated stub
    }
}
